package org.fing.edu.uy.esbadp.core.historic;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.fing.edu.uy.esbadp.core.AdpFlowTree;
import org.fing.edu.uy.esbadp.core.utils.CircularList;

/**
 * 
 * @author dev
 * Administrador de los flows historicos para los servicios.
 */
public class FlowHistoricManager {
	
	private static FlowHistoricManager instance = null;
	
	private HashMap<Long, CircularList<AdpFlowTree>> historico;
	private int amount;
	
		
	private FlowHistoricManager(){
		historico = new HashMap<Long,CircularList<AdpFlowTree>>();
		//TODO como mejora se puede agregar una propiedad para configurar esto.
		amount = 10; 
		
	}
	
	public static synchronized FlowHistoricManager getInstance(){
		if(instance==null){
			instance = new FlowHistoricManager();
		}
		return instance;
	}

		
	public void addFlow(AdpFlowTree flow){
			
			CircularList<AdpFlowTree> serviceHist = historico.get(flow.getServiceID());
			if(serviceHist==null){				
				serviceHist = new CircularList<AdpFlowTree>(amount);
				historico.put(flow.getServiceID(), serviceHist);
			}
			serviceHist.addElement(flow);
	}

	public HashMap<Long, CircularList<AdpFlowTree>> getHistorico() {
		return historico;
	}

	public void setHistorico(HashMap<Long, CircularList<AdpFlowTree>> historico) {
		this.historico = historico;
	}
	
	public List<AdpFlowTree> getHistoricFlowList(Long serviceID){
		
		CircularList<AdpFlowTree> serviceHist = historico.get(serviceID);
		if(serviceHist==null){
			return new LinkedList<AdpFlowTree>();
		}else{
			return serviceHist.elementList();
		}
	}

	public void clearHistoricFlowTreeForService(Long serviceID) {
		historico.remove(serviceID);
	
	}
	

}
